<?php

namespace App\Service;

use App\Models\EwRoles;

class RolesService extends BaseService
{
	public static function getList($params, $count = FALSE)
	{
		$builder = new \Phalcon\Mvc\Model\Query\Builder();
		$builder->columns('DISTINCT(r.id) AS id,r.role_name,r.create_time,r.update_time,r.menu_nodes');
		$builder->addFrom("App\Models\EwRoles", 'r');

		//nodes_name
		if (isset($params->nodes_name) && $params->nodes_name)
			$builder->andwhere("r.nodes_name LIKE :nodes_name:")->bind(['nodes_name' => $params->nodes_name]);

		
		if ($count){
			return $builder->getQuery()->execute()->count();
		}else{
			$builder->limit($params->page_num,($params->p-1)*$params->page_num);
			$result = $builder->getQuery();
			return $result->execute();
		}
	}

	public static function getInfo($id = 0)
	{
		return EwRoles::findFirst($id);
	}

	public static function count($params = NULL)
	{
		return EwRoles::count();
	}

	public static function getCityId($role_id)
	{
		$city_id = 0;
		$info = self::getInfo($role_id);
		if ($info)
			$city_id = $info->city_id;

		return $city_id;
	}

	/**
	 * [save 保存数据]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public static function save($data)
	{
		$row = new EwRoles;
		if (isset($data->id) && $data->id)
			$row = EwRoles::findFirst($data->id);

		return $row->save((array)$data);
	}

	/**
	 * [delete 彻底删除]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function forever($id)
	{
		if (isset($id) && $id)
			$row = EwRoles::findFirst($id);

		if ($row)
			return $row->delete();

		return NULL;
	}
}